package com.mera.tektronix.csviewer.transformers;

import com.mera.tektronix.csviewer.common.dto.EmployeeDto
import com.mera.tektronix.csviewer.domain.*

public class EmployeeTransformer extends AbstractTransformer<EmployeeDto, Employee> {

	@Override
	protected Employee getDomainInstance(EmployeeDto dto) {
		if(dto.id != null) {
			return Employee.get(dto.id);
		}
		return new Employee();
	}

	@Override
	protected EmployeeDto getDtoInstance(Employee domain) {
		return new EmployeeDto();
	}

	@Override
	protected void transformDtoToDomainInternal(EmployeeDto dto, Employee domain) {		
		domain.name = dto.name;
		if(dto.birthDate) domain.birthDate = new Date(dto.birthDate);
		domain.email = dto.email;
		domain.phone = dto.phone;
		if(dto.hiredDate) domain.hiredDate = new Date(dto.hiredDate);
		domain.position = dto.position;
		domain.project = Project.get(dto.projectId);
	}

	@Override
	protected void transformDomainToDtoInternal(Employee domain, EmployeeDto dto) {		
		dto.id = domain.id;
		dto.name = domain.name;
		dto.birthDate = domain.birthDate.format('MM/dd/yyyy');
		dto.email = domain.email;
		dto.phone = domain.phone;
		dto.hiredDate = domain.hiredDate.format('MM/dd/yyyy');
		dto.position = domain.position;
		dto.projectId = domain.project.id;
		dto.projectName = domain.project.name;
		dto.departmentId = domain.project.department.id;
		dto.departmentName = domain.project.department.name;
		dto.photo = domain.getPhotoAsString();
	}

}
